#!/bin/sh

. /lib/functions.sh

logger "$BUTTON pressed for $SEEN seconds"

OVERLAY="$( grep ' /overlay ' /proc/mounts )"

srv_init_mac(){
	apid
	sync
}

srv_for_noac(){
	uci set network.lan.proto=static
	uci set network.lan.ipaddr=19.88.5.26
	uci set network.lan.netmask=255.255.255.0
	uci set dhcp.lan.interface=lan
	uci set dhcp.lan.start=100
	uci set dhcp.lan.limit=50
	uci set dhcp.lan.leasetime=12h
	uci set dhcp.lan.ignore=0
	uci commit network
	uci commit dhcp
	/etc/init.d/dnsmasq restart
	/etc/init.d/firewall disable
	/etc/init.d/firewall stop
	/etc/init.d/network reload
	/etc/init.d/uhttpd start
}

case "$ACTION" in
pressed)
	[ -z "$OVERLAY" ] && return 0

	return 5
;;
timeout)
	. /etc/diag.sh
	set_state failsafe
;;
released)
	if [ "$SEEN" -lt 1 ]
	then
		echo "REBOOT" > /dev/console
		sync
		reboot
	elif [ "$SEEN" -gt 5 ] && [ "$SEEN" -lt 10 ]
	then
		echo "WEB SET" > /dev/console
		srv_init_mac
		srv_for_noac
	elif [ "$SEEN" -gt 15 -a -n "$OVERLAY" ]
	then
		echo "FACTORY RESET" > /dev/console
		jffs2reset -y && reboot &
	fi
;;
esac

return 0
